﻿using System.Runtime.InteropServices;
using System;
using System.Windows;
using System.Drawing.Imaging;
using System.Drawing;
using Daitto.CharpToCpp.Uitil;
using SafeHandle = Daitto.CharpToCpp.Uitil.SafeHandle;
using System.Diagnostics;
using System.Security.Claims;
using System.Security.Policy;
using Prism.Regions;
using Daitto.CharpToCpp.Service;
using Daitto.CharpToCpp.Service.Imple;
using System.Windows.Documents;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Shapes;
using Brushes = System.Windows.Media.Brushes;
using Path = System.Windows.Shapes.Path;

namespace WpfAppTest.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //private readonly IDetectionAlgorithm _detectionAlgorithmImple;

        public MainWindow(IDetectionAlgorithm detectionAlgorithmImple)
        {
            //_detectionAlgorithmImple = detectionAlgorithmImple;
            InitializeComponent();
        }
        //算法测试
        //private unsafe void Button_Click(object sender, RoutedEventArgs e)
        //{

            //string path = String.Empty;
            //Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            //ofd.DefaultExt = ".bmp";
            //ofd.Filter = "bmp file|*.bmp";
            //if (ofd.ShowDialog() == true)
            //{
            //    path = ofd.FileName;
            //}

            //_detectionAlgorithmImple.InitCppMat(path);
            //var t = DateTime.Now;
            //_detectionAlgorithmImple.GetDetection();
            //var ssss = _detectionAlgorithmImple.Bboxs;
            //tx1.Text = ssss.Count.ToString();
            //tx2.Text = (DateTime.Now - t).TotalMilliseconds.ToString();
        //}

        //private void Grid_Loaded(object sender, RoutedEventArgs e)
        //{
        //    Thread.Sleep(1000);
        //    DattoRun();

        //}

        //private async void DattoRun()
        //{
        //    await Task.Run(() =>
        //    {
        //        string[] files = System.IO.Directory.GetFiles(@"D:\1126copy");
        //        List<string> jsonfile = files.Where(o => o.Contains("json")).ToList();
        //        List<string> pngfile = files.Where(o => o.Contains("png") && !o.Contains("json")).ToList();
        //        int i=0;
        //        while (i< jsonfile.Count)
        //        {
        //            JObject jsonObject = null;
        //            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile[i]))
        //            {
        //                using (JsonTextReader reader = new JsonTextReader(file))
        //                {
        //                    jsonObject = (JObject)JToken.ReadFrom(reader);
        //                }
        //            }

        //            string ddd = jsonObject["Objects"][0]["Layers"][0]["Shape"]["Point0"].ToString();
                   

        //            var ddd1 = jsonObject["Objects"][0]["Layers"][0]["Shape"]["Points"].Select(o => o).ToList();
        //            string pathstr = string.Format("M{0}L{1}Z", ddd, string.Join("L", ddd1));
        //            this.Dispatcher.Invoke(() =>
        //            {
        //                gg.Width = (int)double.Parse(jsonObject["Background"]["Width"].ToString());
        //                gg.Height = (int)double.Parse(jsonObject["Background"]["Height"].ToString());
        //                gg.Children.Clear();

        //                System.Windows.Shapes.Path p = new System.Windows.Shapes.Path();
        //                p.Data = Geometry.Parse(pathstr);
        //                p.Fill = Brushes.White;
        //                p.Stretch = Stretch.None;
        //                gg.Children.Add(p);
        //                p.Tag = pngfile[i].Replace(".", "_mask.");
        //                p.Loaded += P_Loaded;
        //            });


        //            //try
        //            //{
                        
        //            //}
        //            //catch (Exception e)
        //            //{
        //            //    var sss = e.Message;
        //            //    continue;
        //            //}
        //            i += 1;
        //            Thread.Sleep(2000);
        //        }
        //    });
        //}

        //private void P_Loaded(object sender, RoutedEventArgs e)
        //{
        //    ConvertToBitmapSource(gg, ((Path)sender).Tag.ToString());
        //}

        //public void ConvertToBitmapSource(UIElement element,string pathstr)
        //{
        //    var target = new RenderTargetBitmap(
        //        (int)element.RenderSize.Width, (int)element.RenderSize.Height,
        //        96, 96, PixelFormats.Pbgra32);
        //    target.Render(element);

        //    var encoder = new PngBitmapEncoder();
        //    var outputFrame = BitmapFrame.Create(target);
        //    encoder.Frames.Add(outputFrame);

        //    using (var file = File.OpenWrite(pathstr))
        //    {
        //        encoder.Save(file);
        //    }
        //}
    }
}
